//
//  PaymentHeader.swift
//  MD5
//
//  Created by 郑东喜 on 2016/11/24.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class PaymentHeader: UIView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    //表格数据源
    lazy var dataArr : NSMutableArray = {
        var data = NSMutableArray()
        data = ["1000","-200","10","49","-40"]
        return data
    }()
    
    var colltionView : UICollectionView?
//    var dataArr = NSMutableArray()//数据源
    let width = UIScreen.main.bounds.size.width//获取屏幕宽
    let height = UIScreen.main.bounds.size.height//获取屏幕高
    
    
    //背景图片
    lazy var BgImg : UIView = {
        let v = UIView()
        v.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 150)
        v.backgroundColor = UIColor.white
        return v
    }()
    
    //线
    func initLine() -> Void {
        let line = UIView()
        line.frame = CGRect(x: 0, y: 45, width: UIScreen.main.bounds.width, height: 1)
        line.backgroundColor = UIColor.gray
        addSubview(line)
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initView()
        initLine()
        initCollectionView()
    }
    
  
    
    func initCollectionView(){
        let layout = UICollectionViewFlowLayout()
        
        //设置collectionView的大小
        colltionView = UICollectionView(frame: CGRect(x: 0, y: 50, width: width, height: 100), collectionViewLayout: layout)


        //注册一个cell
        colltionView!.register(CollectionView.self, forCellWithReuseIdentifier:"cell")
        colltionView?.delegate = self;
        colltionView?.dataSource = self;
        colltionView?.backgroundColor = UIColor.white
        //设置每一个cell的宽高
        layout.itemSize = CGSize(width: (width - 30) / 3, height: 30)
        addSubview(colltionView!)
        
    }

    func initView(){
        
        //背景图片
        addSubview(BgImg)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


extension PaymentHeader {
    //返回多少个组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 1
    }
    //返回多少个cell
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
    }
    //返回自定义的cell
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionView
        var title = String()
        title = dataArr[indexPath.row] as! String

        cell.titleLabel?.setTitle(title, for: .normal)
        cell.titleLabel?.setTitle(title, for: .highlighted)
        
        return cell
    }
}
